package com.hk.service.impl;

import com.hk.dao.INewsDao;
import com.hk.dao.impl.NewsDaoImpl;
import com.hk.model.News;
import com.hk.model.NewsType;
import com.hk.service.INewsService;
import com.hk.service.INewsTypeService;
import com.hk.utils.DateUtils;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/16 10:34
 * @Statement: 新闻Service层接口实现类
 */
public class NewsServiceImpl implements INewsService {

    INewsDao iNewsDao = new NewsDaoImpl();

    INewsTypeService iNewsTypeService = new NewsTypeServiceImpl();

    @Override
    public List<News> findAll(Integer page, Integer limit, News t) {
        ResultSet resultSet = iNewsDao.findAll(page, limit, t);
        List<News> newsList = transverter(resultSet);
//        查询所有新闻类型,匹配对应新闻类型
        List<NewsType> newsTypeList = iNewsTypeService.findAll(-1, -1, new NewsType());
//        遍历新闻
        for (News news : newsList) {
//            遍历类型
            for (NewsType newsType : newsTypeList) {
                if (news.getTypeId() == newsType.getId()) {
                    news.setNewsType(newsType.getName());
                    break;
                }
                if (newsType.getNewsTypeList().size() > 0) {
                    getMatching(newsType,news);
                    break;
                }
            }
            if (news.getNewsType() == null) {
                news.setNewsType("暂无类型");
            }
        }
        //        释放资源
        JdbcMysql.jdbcClose();
        return newsList;
    }

    /**
     * 递归判断新闻属于那个类型
     * @param newsTypeList
     * @param news 存储匹配的节点名称
     */
    public void getMatching(NewsType newsTypeList,News news) {
//        判断是否具有子节点
        if (newsTypeList.getNewsTypeList().size() > 0) {
            for (NewsType type : newsTypeList.getNewsTypeList()) {
                if (news.getTypeId() == type.getId()) {
                    news.setNewsType(type.getName());
                    break;
                }
//                未匹配到继续递归搜索
                getMatching(type,news);
            }
        }
    }

    @Override
    public int count(Integer page, Integer limit, News t) {
        ResultSet resultSet = iNewsDao.count(page,limit, t);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public News findById(Integer id) {
        ResultSet resultSet = iNewsDao.findById(id);
        List<News> newsList = transverter(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return newsList.get(0);
    }

    @Override
    public int save(News t) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = iNewsDao.save(t);
//       关闭资源
        JdbcMysql.jdbcClose();
        if (save == 1) {
            System.out.println("添加成功");
        }
        return save;
    }

    @Override
    public int update(News t) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = iNewsDao.update(t);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (update == 1) {
            System.out.println("新闻信息修改成功");
        }
        return update;
    }

    @Override
    public int delete(Integer id) {
        int delete = iNewsDao.delete(id);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (delete == 1) {
            System.out.println("新闻信息删除成功");
        }
        return delete;
    }

    /**
     * 将传递的数据进行转换成集合
     * @param resultSet
     * @return
     */
    public List<News> transverter(ResultSet resultSet ) {
        List<News> newsList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String subhead = resultSet.getString("subhead");
                String content = resultSet.getString("content");
                String releaseDate = resultSet.getString("release_date");
                String author = resultSet.getString("author");
                int hits = resultSet.getInt("hits");
                int typeId = resultSet.getInt("type_id");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                News news = new News(id,title,subhead,content,releaseDate,author,hits,typeId,gmtCreate,gmtModified);
                newsList.add(news);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return newsList;

    }

}
